package com.jse;

import java.util.EnumSet;

import javax.servlet.DispatcherType;
import javax.servlet.FilterRegistration;
import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.DispatcherServlet;

public class WebXml implements WebApplicationInitializer{

    public void onStartup(ServletContext sc) throws ServletException {
    	AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();  
    	ctx.register(AppConfig.class);
    	ctx.register(WebConfig.class);
        sc.addListener(new ContextLoaderListener(ctx));  
    	ServletRegistration.Dynamic dispatcher = sc.addServlet("dispatcher", new DispatcherServlet(ctx));
        dispatcher.setLoadOnStartup(1);
        String str = sc.getRealPath("/").replace("\\", "/");
        String webPath = str.substring(0, str.indexOf("tomcat") + 7) + "webapps/";
        dispatcher.setMultipartConfig(new MultipartConfigElement(webPath+"upload/temp",31457280,314572800, 0));
        dispatcher.addMapping("/");
        FilterRegistration.Dynamic charfilter=sc.addFilter("characterEncodingFilter", CharacterEncodingFilter.class);
        charfilter.setInitParameter("encoding", "UTF-8");
        charfilter.setInitParameter("forceEncoding", "true");
        charfilter.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST,DispatcherType.FORWARD), true, "/*");
        sc.getServletRegistration("default").addMapping("*.jsp","*.png","*.gif","*.jpg","*.jpeg","*.bmp","*.js","*.map",
        "*.css","*.jspx","*.jpeg","*.htm","*.xhtml","*.mp3","*.mp4","*.ico","*.ttf","*.svg","*.jar","*.woff","*.eot","*.ico");
    }
    
}